//
//  SEGAddPIcturesView.m
//  Commom
//
//  Created by 揭婧雯 on 2019/6/4.
//

#import "SEGAddPIcturesView.h"

#import <Masonry/Masonry.h>

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

static const NSInteger maxPicNumberInRow = 3;//一行的最大图片数

@implementation SEGAddPIcturesView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUpUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    self.savePicArray = [[NSMutableArray alloc] init];
    self.photoMaxCount = 9;//默认为9
    self.pic_Padding = 13.0 * kScaleWidth;
    self.imageHeight = (LYScreenWidth() - 50 - (maxPicNumberInRow - 1) * self.pic_Padding) / maxPicNumberInRow;
    
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.top.equalTo(self);
        make.width.mas_equalTo(self.imageHeight);
        make.height.mas_equalTo(self.imageHeight);
    }];
}

/**
 * 刷新图片界面
 * @author Wendy
 */
- (void)updatePicContentView
{
    //移除子视图
    [self.subviews enumerateObjectsUsingBlock:^(UIView * obj, NSUInteger idx, BOOL *stop) {
        //添加按钮不移除
        if(![obj isEqual:self.addButton]) {
            [obj removeFromSuperview];
        }
    }];

    [self.savePicArray enumerateObjectsUsingBlock:^(UIImage *obj, NSUInteger idx, BOOL *stop) {

        CGFloat leftPadding = (idx % maxPicNumberInRow) * (self.pic_Padding + self.imageHeight);
        CGFloat topPadding = (idx / maxPicNumberInRow) * (self.pic_Padding + self.imageHeight);

        UIImageView* picImageView = [[UIImageView alloc] init];
        [picImageView setImage:obj];
        [picImageView.layer setMasksToBounds:YES];
        //        picImageView.layer.cornerRadius = 3;
        picImageView.contentMode = UIViewContentModeScaleAspectFill;
        picImageView.userInteractionEnabled = YES;
        [self addSubview:picImageView];
        [picImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(leftPadding);
            make.top.equalTo(self).with.offset(topPadding);
            make.width.height.mas_equalTo(self.imageHeight);
        }];

        // 显示删除按钮
        UIImage* closeImage = [UIImage imageWithContentsOfFile:SEGPicName(@"delete2")];
        UIButton* closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [closeBtn setImage:closeImage forState:UIControlStateNormal];
        [closeBtn addTarget:self action:@selector(seg_removeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        closeBtn.tag = 100 + idx;
        [self addSubview:closeBtn];
        [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(picImageView.mas_right).offset(7);
            make.top.equalTo(picImageView.mas_top).offset(-7);
            //            make.width.height.mas_equalTo(30.0);//图片范围17.0 不好点 加大点击区域
        }];

    }];

    [self setAddButtonWithArray:self.savePicArray];
    [self layoutIfNeeded];
}

- (void)setAddButtonWithArray:(NSArray *)array
{
    NSInteger count = array.count;
    if (count >= self.photoMaxCount) {
        self.addButton.hidden = YES;
    }else {
        self.addButton.hidden = NO;
    }
    if (count == self.photoMaxCount) {
        count = self.photoMaxCount - 1;//图片数达到最大时，实际是不显示添加按钮的，所以将其与最后一张图片重叠布局
    }
    CGFloat leftPadding = (count % maxPicNumberInRow) * (self.pic_Padding + self.imageHeight);
    CGFloat topPadding = (count / maxPicNumberInRow) * (self.pic_Padding + self.imageHeight);
    [self.addButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(leftPadding);
        make.top.equalTo(self).offset(topPadding);
        make.width.height.mas_equalTo(self.imageHeight);
    }];
    
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(topPadding + self.imageHeight);
    }];
}

- (void)seg_addButtonPressed
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(addPictureButtonClick)]) {
        [self.delegate addPictureButtonClick];
    }
}

- (void)seg_removeBtnAction:(UIButton*)sender
{
    NSUInteger index = sender.tag - 100;
    [self.savePicArray removeObjectAtIndex:index];
    if (self.delegate && [self.delegate respondsToSelector:@selector(removePictureWithIndex:)]) {
        [self.delegate removePictureWithIndex:index];
    }
    [self updatePicContentView];
}


- (UIButton *)addButton
{
    if (!_addButton) {
        _addButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addButton setBackgroundImage:[UIImage imageWithContentsOfFile:SEGPicName(@"upload_pictures")] forState:UIControlStateNormal];
        [_addButton addTarget:self action:@selector(seg_addButtonPressed) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_addButton];
    }
    return _addButton;
}

@end
